package com.hb.core.oauth2;

import lombok.extern.slf4j.Slf4j;
import org.apache.http.HttpStatus;
import org.springframework.security.access.AccessDeniedException;
import org.springframework.security.web.access.AccessDeniedHandler;
import org.springframework.stereotype.Component;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

/**
 * @description: 通用权限不足处理
 * @projectName: zhb-frame-build
 * @author: zhouhaizhi
 * @createTime: 2021/8/27 18:21
 * @version:1.0
 */
@Slf4j
@Component("customAccessDeniedHandler")
public class CustomAccessDeniedHandler implements AccessDeniedHandler {

    @Override
    public void handle(HttpServletRequest httpServletRequest, HttpServletResponse httpServletResponse, AccessDeniedException exception) throws IOException, ServletException {
        log.info("------通用无权限异常处理------，e-->{}", exception);
        httpServletResponse.setStatus(HttpStatus.SC_FORBIDDEN);
        httpServletResponse.setHeader("Content-Type", "application/json;charset=UTF-8");
        try {
            httpServletResponse.getWriter().write("No Permission");
        } catch (IOException e) {
            log.warn("通用无权限异常处理出现异常", e);
        }

    }
}
